package com.efounder.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.efounder.activity.CommentActivity;
import com.efounder.activity.ImageActivity;
import com.efounder.activity.MallWebViewActivity;
import com.efounder.activity.ProductDetailsActivity;
import com.efounder.bean.BaseItem;
import com.efounder.bean.Collection;
import com.efounder.bean.CommentDatas;
import com.efounder.bean.DetailsDatas;
import com.efounder.mobilemall.R;
import com.efounder.utils.ResStringUtil;
import com.lzy.ninegrid.NineGridView;
import com.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wei on 2018/1/10/010.
 */

public class ScrollAdapter extends RecyclerView.Adapter<ScrollAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private List<BaseItem> mDatas;
    private List<CommentDatas.Comments.CommentPics> comments;
    private Context mContext;
    private int layoutId;
    private View footView;
    public static final int NORMAL = 1;
    public static final int FOOT = 2;
    private List<CommentDatas.Comments> commentsData;
    private ArrayList<String> list;
    private CommentDatas commentDatas;
    private DetailsDatas detailsDatas;

    public ScrollAdapter(Context context, List<BaseItem> datats, int layoutId) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
        this.layoutId = layoutId;
//        initData();
    }

    public ScrollAdapter(List<CommentDatas.Comments.CommentPics> commentPics, int layoutId, Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        comments = commentPics;
        this.layoutId = layoutId;
        list = new ArrayList<>();
        for (int i = 0; i < commentPics.size(); i++) {
            list.add(commentPics.get(i).getCommentPicUrl());
        }
    }

    //List<CommentDatas.Comments> comments
    public ScrollAdapter(CommentDatas commentData1, DetailsDatas datas, Context context, int layoutId) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        commentDatas = commentData1;
        commentsData = commentData1.getComments();
        detailsDatas = datas;
        this.layoutId = layoutId;
    }

    public void addFootView(View footView) {
        this.footView = footView;
    }

    public int getFootViewCount() {
        return footView == null ? 0 : 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (layoutId == R.layout.item_comments_scroll) {
            if (position < comments.size()) {
                return NORMAL;
            }
        } else if (layoutId == R.layout.item_scroll_comments) {
            if (position < commentsData.size()) {
                return NORMAL;
            }
        } else {
            if (position >= mDatas.size()) {
                return FOOT;
            }
            return NORMAL;
        }
        return NORMAL;
    }

    @Override
    public int getItemCount() {
        if (layoutId == R.layout.item_comments_scroll) {
            return comments.size();
        } else if (layoutId == R.layout.item_scroll_comments) {
            return commentsData.size();
        } else {
//            return mDatas.size() + getFootViewCount();
            return mDatas.size();
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View viewFoot = mInflater.inflate(R.layout.view_foot, viewGroup, false);
        if (viewType == FOOT) {
            return new ViewHolder(viewFoot);
        }
        View view = mInflater.inflate(layoutId, viewGroup, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        if (getItemViewType(i) == NORMAL) {
            if (layoutId == R.layout.item_scroll) {
                String imgUrl = mDatas.get(i).getImgUrl();
                Glide.with(mContext)
                        .load(imgUrl)
                        .apply(new RequestOptions().centerCrop()
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .skipMemoryCache(false))
                        .into(viewHolder.mImg);
                viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        Intent intent = new Intent(mContext, MallWebViewActivity.class);
//                        Bundle bundle = new Bundle();
//                        bundle.putString("urlstr", mDatas.get(i).getIntent());
//                        intent.putExtras(bundle);
//                        ((Activity) mContext).startActivity(intent);
                    }
                });
            } else if (layoutId == R.layout.item_comments_scroll) {
                String imgUrl = comments.get(i).getCommentPicUrl();
                Glide.with(mContext)
                        .load(imgUrl)
                        .apply(new RequestOptions().centerCrop()
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .skipMemoryCache(false))
                        .into(viewHolder.mImg);
                viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        intent.putStringArrayListExtra("pic", list);
                        intent.putExtra("position", i);
                        intent.setClass(mContext, ImageActivity.class);
                        mContext.startActivity(intent);
                    }
                });
            } else if (layoutId == R.layout.item_scroll_comments) {
                viewHolder.item_comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            Intent intent = new Intent(mContext, CommentActivity.class);
                            intent.putExtra("goodbh", detailsDatas.getGood().getGoodId());
                            intent.putExtra("count", commentDatas.getCommentCount());
                            double goodPrice = detailsDatas.getGood().getMallGoodPrice().getPrice();
                            intent.putExtra("goodPrice", goodPrice);
                            intent.putExtra("position", i);
//                        intent.putExtra("isCollection", detailsDatas.getIsCollection() == 1 ? true : false);
                            intent.putExtra("isCollection", Collection.isLove());
                            ((Activity) (mContext)).startActivityForResult(intent, 2);
                        } catch (Exception e) {
                            e.printStackTrace();
                            ToastUtils.showShort(ResStringUtil.getString(R.string.common_text_http_request_data_fail));
                        }

                    }
                });
                String head = commentsData.get(i).getUserAvatar();
                if (commentsData.get(i).getCommentPics().size() > 0) {
                    viewHolder.image.setVisibility(View.VISIBLE);
                    viewHolder.imageCount.setVisibility(View.VISIBLE);
                    String image = commentsData.get(i).getCommentPics().get(0).getCommentPicUrl();
                    viewHolder.imageCount.setText(commentsData.get(i).getCommentPics().size() + ResStringUtil.getString(R.string.mail_zhang));
                    Glide.with(mContext)
                            .load(image)
                            .apply(new RequestOptions().centerCrop()
                                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                                    .skipMemoryCache(false))
                            .into(viewHolder.image);
                } else {
                    viewHolder.image.setVisibility(View.GONE);
                    viewHolder.imageCount.setVisibility(View.GONE);
                    android.view.ViewGroup.LayoutParams lp = viewHolder.rv_comment.getLayoutParams();
                    lp.width = dip2px(240);
                    viewHolder.rv_comment.setLayoutParams(lp);
//                    viewHolder.rv_comment.setLayoutParams(new RelativeLayout.LayoutParams(dip2px(240),dip2px(120)));
                }

                Glide.with(mContext)
                        .load(head)
                        .apply(new RequestOptions().transform(new CircleCrop()))
                        .into(viewHolder.head);

//                Glide.with(mContext)
//                        .load(head)
//                        .apply(new RequestOptions().centerCrop()
//                                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                                .skipMemoryCache(false))
//                        .into(viewHolder.head);

                viewHolder.name.setText(commentsData.get(i).getUserName());
                viewHolder.content.setText(commentsData.get(i).getContent());
            } else if (layoutId == R.layout.item_scroll_brand) {
                String logoUrl = mDatas.get(i).getLogoUrl();
                String iconUrl = mDatas.get(i).getIconUrl();
                String name = mDatas.get(i).getName();
                String introduce = mDatas.get(i).getIntroduce();
                Glide.with(mContext)
                        .load(logoUrl)
                        .apply(new RequestOptions().centerCrop()
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .skipMemoryCache(false))
                        .into(viewHolder.mImg1);
                Glide.with(mContext)
                        .load(iconUrl)
                        .apply(new RequestOptions().centerCrop()
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .skipMemoryCache(false))
                        .into(viewHolder.mImg2);
                viewHolder.mTxt1.setText(name);
                viewHolder.mTxt2.setText(introduce);
                viewHolder.mImg1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(mContext, MallWebViewActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("urlstr", mDatas.get(i).getIntent());
                        intent.putExtras(bundle);
                        ((Activity) mContext).startActivity(intent);
                    }
                });
            } else if (layoutId == R.layout.item_scroll_price) {
                String title = (String) mDatas.get(i).getTitle();
                String currentPrice = mDatas.get(i).getCurrentPrice();
                String imgUrl = (String) mDatas.get(i).getImgUrl();
//                String singleBenefitPoint = (String) mDatas.get(i).getSingleBenefitPoint();
                Glide.with(mContext)
                        .load(imgUrl)
                        .apply(new RequestOptions().centerCrop()
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .skipMemoryCache(false))
                        .into(viewHolder.mImg1);
                Glide.with(mContext).load(imgUrl).into(viewHolder.mImg1);
//                if (singleBenefitPoint != null && singleBenefitPoint.length() > 0) {
//                    viewHolder.mTxt.setText(singleBenefitPoint);
//                    viewHolder.mTxt.setVisibility(View.VISIBLE);
//                }
                viewHolder.mTxt1.setText(title);
                viewHolder.mTxt2.setText(mDatas.get(i).getPriceUnit() + " " + currentPrice);
                viewHolder.mImg1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        intent.putExtra("goodbh", mDatas.get(i).getItemId());
                        intent.setClass(mContext, ProductDetailsActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    }
                });
            }
        }


    }

    public int dip2px(float dpValue) {
        float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private class GlideImageLoader implements NineGridView.ImageLoader {
        @Override
        public void onDisplayImage(Context context, ImageView imageView, String url) {
            Glide.with(context).load(url).into(imageView);
        }

        @Override
        public Bitmap getCacheImage(String url) {
            return null;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mImg;
        ImageView mImg1;
        ImageView mImg2;
        //        TextView mTxt;
        TextView mTxt1;
        TextView mTxt2;
        TextView name;
        TextView content;
        TextView imageCount;
        ImageView tvFootView;
        ImageView head;
        ImageView image;
        RelativeLayout rv_comment;
        RelativeLayout item_comment;

        public ViewHolder(View view) {
            super(view);
            if (view == footView) {
                tvFootView = (ImageView) view.findViewById(R.id.footer);
            } else {
                if (layoutId == R.layout.item_scroll) {
                    mImg = (ImageView) view.findViewById(R.id.imageView);
                } else if (layoutId == R.layout.item_comments_scroll) {
                    mImg = (ImageView) view.findViewById(R.id.imageView);
                } else if (layoutId == R.layout.item_scroll_comments) {
                    head = (ImageView) view.findViewById(R.id.head);
                    name = (TextView) view.findViewById(R.id.name);
                    content = (TextView) view.findViewById(R.id.content);
                    image = (ImageView) view.findViewById(R.id.image);
                    rv_comment = view.findViewById(R.id.rv_comment);
                    imageCount = view.findViewById(R.id.imageCount);
                    item_comment = view.findViewById(R.id.item_comment);
                } else if (layoutId == R.layout.item_scroll_brand) {
                    mImg1 = (ImageView) view.findViewById(R.id.imageView1);
                    mImg2 = (ImageView) view.findViewById(R.id.imageView2);
                    mTxt1 = (TextView) view.findViewById(R.id.textView1);
                    mTxt2 = (TextView) view.findViewById(R.id.textView2);
                } else if (layoutId == R.layout.item_scroll_price) {
                    mImg1 = (ImageView) view.findViewById(R.id.imageView1);
//                    mTxt = (TextView) view.findViewById(R.id.text);
                    mTxt1 = (TextView) view.findViewById(R.id.textView1);
                    mTxt2 = (TextView) view.findViewById(R.id.textView2);
                }

            }

        }
    }

}
